#!/usr/bin/env python
# -*- coding: utf-8 -*-

from config import Config

config = Config()


def getPlugins(method, path):
    # obtenemos listado de plugins
    plugins = [item.strip(' ') for item in config.get('plugins').split(',')]
    
    P = []
    for plugin in plugins:
        m = __import__('..plugins.'+plugin)
        P.append( m.plugin(method, path) )
        
    return P



class MeliponiniPlugin():
    def __init__(self, method, path):
        self.path = path

        self.httphandlers = []
        self.timers = []
        
        self.data = None
        self.response = None
        self.code = 200
        self.filename = None
        self.filetype = None
        
        self.constructor(method)
        
        
    def constructor(self, method):
        return
    
    def getTimers(self):
        return self.timers
        
    def getHandlers(self):
        return self.httphandlers
        
    def addHTTPHanler(self, match, method):
        self.httphandlers.append({'match':match, 'method':method})
        
        
    def addTimer(self, seconds, method):
        self.timers.append({'seconds':seconds, 'method':method})
        
    def responseJSON(self, data, code):
        if code == None:
            code = 200
            
        self.response = 'json'
        self.data = data
        self.code = code
        
    def response401(self):
        self.response = '500'
        self.data = None
        self.code = 500
    
    def response404(self):
        self.response = '500'
        self.data = None
        self.code = 500

    def response500(self):
        self.response = '500'
        self.data = None
        self.code = 500
        
    def responseFile(self, data, filename, type):
        self.response = 'file'
        self.data = data
        self.code = 200
        self.filename = filename
        self.filetype = type
        
        